//
//  AttentionViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "AttentionViewController.h"
#import "AttentionCell.h"
@interface AttentionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,assign)NSInteger pageNumber;//页码
@end

@implementation AttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    [self getFollowList];
}

-(void)creatView
{
    [self initTitleWithTitle:@"我的关注"];
    _dataArr = [NSMutableArray new];
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 60*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //加载cell
    AttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AttentionCell"];
    if (cell == nil) {
        cell = [[AttentionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AttentionCell"];
    }
    [cell setDataDic:_dataArr[indexPath.row]];
    __weak typeof(self) weakSelf = self;
    cell.blcok = ^(){
        [weakSelf cancelFollowWithUserId:weakSelf.dataArr[indexPath.row][@"othersUserId"]];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void)getFollowList
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/followList" Dic:@{@"id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [_dataArr addObjectsFromArray:responseDic[@"data"]];
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    [_tableV.mj_footer endRefreshing];
                }
            });
        }];
    });
}

-(void)cancelFollowWithUserId:(NSString *)userId
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/cancelFollow" Dic:@{@"othersUser.id":userId,@"ownUser.id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [TomamaUI creatAlertWithTarget:self message:@"取关成功" timeInterval:2];
                    for (NSDictionary *dic in _dataArr) {
                        if ([dic[@"othersUserId"] isEqualToString:userId]) {
                            [_dataArr removeObject:dic];
                            break;
                        }
                    }
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
